package com.swlz.servlet.admin;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.swlz.entity.MallProduct;
import com.swlz.entity.WebFile;
import com.swlz.service.MallProductService;
import com.swlz.service.MallTypeService;
import com.swlz.service.WebFileService;
import com.swlz.service.impl.MallProductServiceImpl;
import com.swlz.service.impl.MallTypeServiceImpl;
import com.swlz.service.impl.WebFileServiceForGoFastDFS;

import cn.hutool.core.io.FileUtil;


//admin/product.let?action=updateProduct

@WebServlet("/admin/upload.let")
@MultipartConfig(
		maxFileSize = -1,
		maxRequestSize = -1
)
public class UploadServlet extends HttpServlet{

	protected Logger log = LoggerFactory.getLogger(this.getClass());

	private WebFileService webFileService = new WebFileServiceForGoFastDFS();

	private MallTypeService malltypeService =new MallTypeServiceImpl();
	private MallProductService mallProductService=new MallProductServiceImpl();
	 
		@Override
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			MallProduct p = new MallProduct();
			p.setProductId(Long.valueOf(request.getParameter("productId")));
			p.setProductName(request.getParameter("productName"));
			
			int i = Integer.valueOf(request.getParameter("productNum"));
			p.setProductNum(i);
			
			p.setProductPrice2(Float.valueOf(request.getParameter("productPrice2")));
			p.setProductColors(request.getParameter("productColors"));
			
		
//			upload(request, response, p);
			log.debug("文件上传开始");
			Map<String,Object> ret = new HashMap<String, Object>();
			try {
				File root = new File(getServletContext().getRealPath("/"));
				log.debug("获取网站的根目录：{}",root);
				
				String uri = "front/images/";
				new File(root,uri).mkdirs();
				log.debug("创建保存文件的目录：{}",uri);
				
				
				Part img = request.getPart("productImgs");//获取上传的文件
				
				String ext = FileUtil.extName(img.getSubmittedFileName());
				log.debug("获取上传文件的扩展名：{}",ext);
				
				uri += UUID.randomUUID()+"."+ext;
				log.debug("保存的文件：{}",uri);
				
				p.setProductImgs(uri);
				log.debug("具体保存的文件：{}",new File(root,uri));
				img.write(new File(root,uri).toString());
				
				ret.put("ok", true);
				ret.put("uri",uri);
			}catch (Exception e) {
				log.warn("文件上传失败",e);
				ret.put("ok", false);
				ret.put("message", e.getMessage());
			}
			
			response.reset();
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().print(JSON.toJSONString(ret));
			response.flushBuffer();
			log.debug("文件上传结束");
			
			log.debug(p.toString());
			mallProductService.update(p);
			
//			 request.getRequestDispatcher("/admin/product/productPage.jsp").forward(request, response);
		}
}
